
############################################
##### QCA on Authoritarian Peristence  #####
############################################


rm(list = ls())

# Set working directory:
setwd()

## NOTE: For correct replications, please load the packages QCA 2.2 and SetMethods 2.0

library(QCA); library(QCAGUI); library(SetMethods); library(lattice);
library(arm); library(plyr); library(car); library(stringr); 
library(xtable)

# load prepared and calibrated data (see previous script)
load("calibratedata.rda")


# get overview of data
head(data)


# Outcome: persistent regimes ------------------------------------------------------------#
# ----------------------------------------------------------------------------------------#



# Analysis of necessity ------------------------------------------------------------------#

QCAfit(data[, 6:11], data$pers, necessity = TRUE, names(data[, 6:11]))
QCAfit(1-data[, 6:11], data$pers, necessity = TRUE, paste("not", names(data[, 6:11])))

SUIN <- superSubset(data, outcome = "pers",
            conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
            incl.cut = 0.9, cov.cut = 0.7,
            necessity = TRUE)
SUIN

# there are two conjunction with reasonable consistency and coverage values
# but too low RoN values (cf. paper)


# Analysis of sufficiency ----------------------------------------------------------------#

# truth Table
TT_y <- truthTable(data, outcome = "pers",
                   conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                   incl.cut1 = 0.77,
                   n.cut = 1,
                   show.cases = TRUE,
                   details = TRUE,
                   complete = FALSE,
                   sort.by = c("OUT", "incl", "n"))

TT_y
# set consistency threshold at 0.77 (cf. arguments in paper)

# conservative solution
sol_y_c <- eqmcc(data, outcome = "pers",
                 conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                 incl.cut1 = 0.77,
                 n.cut = 1,
                 show.cases = TRUE,
                 details = TRUE)
sol_y_c


# most parsimonious solution
sol_y_p <- eqmcc(data, outcome = "pers",
                 conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                 incl.cut1 = 0.77,
                 n.cut = 1,
                 show.cases = TRUE,
                 details = TRUE,
                 row.dom = TRUE,
                 include = "?")
sol_y_p

# simplifying assumptions
sol_y_p$SA

# intermediate solution
sol_y_i <- eqmcc(data, outcome = "pers",
                 conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                 incl.cut1 = 0.77,
                 n.cut = 1,
                 show.cases = TRUE,
                 details = TRUE,
                 include = "?",
                 dir.exp = c("-",1,1,1,1,1))
sol_y_i

# dir. ex.: all conds are expected to be present in all persistent regimes beside repp due 
# to its backfiring effects (cf. paper)

# easy counterfactuals
sol_y_i$i.sol$C1P1$EC

# check the cases deviant in coverage
devcov <- cases.suf.dcv(sol_y_i, "PERS")
devcov



# Negation of outcome: nonpersistent regimes ----------------------------------------------#
# -----------------------------------------------------------------------------------------#


# Analysis of necessity -------------------------------------------------------------------#

QCAfit(data[, 6:11], data$pers, necessity = TRUE, names(data[, 6:11]), negation = TRUE)
QCAfit(1-data[, 6:11], data$pers, necessity = TRUE, paste("not", names(data[, 6:11])), negation = TRUE)

superSubset(data, outcome = "pers", neg.out = TRUE,
                      conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                      incl.cut = 0.9, cov.cut = 0.7)


# Analysis of sufficiency ----------------------------------------------------------------#

# truth table
TT_ny <- truthTable(data, outcome = "pers", neg.out = TRUE,
                    conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                    incl.cut1 = 0.4, 
                    n.cut = 1,
                    show.cases = TRUE,
                    complete = FALSE,
                    sort.by = c("OUT", "incl", "n"))

TT_ny

# no row passes the minimum consist. threshold at 0.75
# this supports our findings in Schneider and Maerz (forthcoming)
# the three pillars and also the modified version of it - the hexagon - 
# explain autocratic persistence, but not autocratic breakdown

# logical minimization (for the sake of it - yet, note of caution in the paper,
# since the threshold of 0.4 is clearly too low for receiving solid results)

# conservative
sol_ny_c <- eqmcc(data, outcome = "pers", neg.out = TRUE,
                  conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                  incl.cut1 = 0.4, 
                  n.cut = 1,
                  show.cases = TRUE,
                  details = TRUE)
sol_ny_c


# parsimonious
sol_ny_p <- eqmcc(data, outcome = "pers", neg.out = TRUE,
                  conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                  incl.cut1 = 0.4, 
                  n.cut = 1,
                  show.cases = TRUE,
                  details = TRUE, include = "?")
sol_ny_p

# simplifying assumptions
sol_ny_p$SA

# intermediate solution
sol_ny_i <- eqmcc(data, outcome = "pers", neg.out = TRUE,
                  conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                  incl.cut1 = 0.4, 
                  n.cut = 1,
                  show.cases = TRUE,
                  details = TRUE, include = "?", 
                  dir.exp = c("-","-","-","-","-","-"))
sol_ny_i

# easy counterfactuals
sol_ny_i$i.sol$C1P1$EC


# Enhanced Standard Analysis (ESA) --------------------------------------------------------#
# -----------------------------------------------------------------------------------------#


# 1. CHECK: any SA contradicting the statement of necessity?
# does not apply since no condition or conjunction of condition is declared necessary

# 2. CHECK: any contradictory assumptions?
# idea: none of the SAs should simultaniously 
# assume sufficiency for y and -y 

CSA_p <- intersect(rownames(sol_y_p$SA$M1), rownames(sol_ny_p$SA$M1))
CSA_p

CSA_i <- intersect(rownames(sol_y_i$i.sol$C1P1$EC), rownames(sol_ny_i$i.sol$C1P1$EC))
CSA_i

# the parsimonious relies on two contradictory rows: 50, 58
# lets exclude them from the parsimonious
sol_y_p <- eqmcc(data, outcome = "pers",
                 conditions = c("repp", "repc","coopv", "coops", "legd", "legs"),
                 incl.cut1 = 0.77,
                 n.cut = 1,
                 show.cases = TRUE,
                 details = TRUE,
                 row.dom = TRUE,
                 include = "?",
                 omit = c(50,58))
sol_y_p

# simplifying assumptions
sol_y_p$SA
# results in model ambiguity (4 models)
# furthermore, maybe this step is also not really needed
# since the minimalization process of "no authoritarian persistence" 
# is not really made anyway and hence,
# row 50, 58 are only used for interpretating the sol_y_p


# 3. CHECK: simultaneous subset relations for Y and -Y?

# idea: none of the subsets should simultaniously 
# assume sufficiency for y and -y 

# NOTE: difference to step 2 is that this can also concern
# the rows which are NOT logical remainders!

SSR<-intersect(rownames(TT_y$tt)[TT_y$tt$OUT==1],rownames(TT_ny$tt)[TT_ny$tt$OUT==1])
SSR
# ok, no simultaneous subset relations present
# end of ESA
# summary: no changes made in sol_y_p or sol_y_i

# end of analysis (robustness tests in separate scripts!) ---------------------------------------#
# -----------------------------------------------------------------------------------------------#
